package com.fjrz.myapplication

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.ProgressListener
import com.module.lemlin.http.SubscribeViewModel
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import rxhttp.wrapper.entity.Progress
import java.io.File

open class BaseViewModel(application: Application) : OwnerViewModel(application) {

    private val mBaseManager: BaseManager = BaseManager()

    val uploadLiveData = MutableLiveData<HttpResponseBody<Any>>()
    val progressLiveData = MutableLiveData<Progress>()

    fun upload(url: String, path: String) {
        setRxLifeScope(object :SubscribeViewModel<Any>(uploadLiveData){
            override fun onFailure(throwable: Throwable) {
                super.onFailure(throwable)
                LogUtils.e("=============${throwable.msg}")
            }
        }) {
            val mProgress = object : ProgressListener {
                override fun onProgress(progress: Progress) {
                    progressLiveData.postValue(progress)
                    LogUtils.e(progress.progress)
                }
            }
            return@setRxLifeScope mBaseManager.upload(url, path, mProgress)
        }
    }


    val userSmsLiveData = MutableLiveData<HttpResponseBody<String>>()

    fun test1() {
        setViewModelScope(object : SubscribeViewModel<String>(userSmsLiveData) {
            override fun onSuccess(resp: String) {
                super.onSuccess(resp)
            }
        }) {
            testWithContext()
        }
    }

    suspend fun testWithContext(): String = withContext(Dispatchers.IO) {
        "123456"
    }
}